//
//  ExaminaColumnController.m
//  Coach_HaiGeEr
//
//  Created by liuchen on 2018/10/17.
//  Copyright © 2018年 海格尔. All rights reserved.
//

#import "ExaminaColumnController.h"
#import "ExaminaExamineController.h"
#import "ExaminaStatisticsController.h"
#import "ExaminaWaitExamineController.h"
#import "ExaminaColumnTopView.h"
#import "ExaminaColumnReasonView.h"
#import "ExaminaColumnHeaderWithSectionView.h"
#import "ExaminaColumnFirstCell.h"
#import "TeachManagementModel.h"
#import "MJExtension.h"

@interface ExaminaColumnController ()<UITableViewDelegate,UITableViewDataSource,ExaminaColumnFirstCellDelegate,ExaminaColumnReasonViewDelegate>
@property (nonatomic,strong) UITableView *examinaWithTodayTableView;
@property (nonatomic,strong) NSMutableArray *subjectTwoArr;
@property (nonatomic,strong) NSMutableArray *subjectThreeArr;
@property (nonatomic,strong) NSMutableArray *tempSubjectTwoArr;
@property (nonatomic,strong) NSMutableArray *tempSubjectThreeArr;
@property (nonatomic,strong) NSMutableDictionary *resonDic;
@property (nonatomic,strong) ExaminaColumnReasonView *reasonView;
@property (nonatomic,strong) ExaminaColumnTopView *topView;
@property (nonatomic,strong) ExaminaColumnHeaderWithSectionView *hederWithOneSectionView;
@property (nonatomic,strong) ExaminaColumnHeaderWithSectionView *hederWithTwoSectionView;
@end

@implementation ExaminaColumnController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[CoachTabBarController shareLZHTabbarController].tabBar setHidden:YES];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"考试管理";
    [self setNavBack];
    [self createSubView];
}

#pragma mark ==========创建视图==========
- (void)createSubView{
    self.view.backgroundColor = [UIColor colorWithHexString:@"#eeeeee"];
    [self.view addSubview:self.examinaWithTodayTableView];
    [self loadData];
}

#pragma mark ==========数据加载==========
- (void)loadData{
    [self showLoading];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd";
    [[NetworkEngine sharedClient] GETByToken:EXAMINAMANAGEMENTTODAYList data:[NSDictionary dictionaryWithObjectsAndKeys:[dateFormatter stringFromDate:[NSDate date]],@"date", nil] success:^(id responseObject) {
        [self hideHUD];
        if ([[responseObject objectForKey:@"code"] integerValue]==1) {
            self.subjectTwoArr = [[NSMutableArray alloc] initWithArray:[TeachManagementModel mj_objectArrayWithKeyValuesArray:[[[responseObject objectForKey:@"data"] objectAtIndex:0] objectForKey:@"examInfoList"]]];
            self.subjectThreeArr = [[NSMutableArray alloc] initWithArray:[TeachManagementModel mj_objectArrayWithKeyValuesArray:[[[responseObject objectForKey:@"data"] objectAtIndex:1] objectForKey:@"examInfoList"]]];
            [self.examinaWithTodayTableView reloadData];
        } else if ([[responseObject objectForKey:@"code"] integerValue]==2) {
            [self toLoginVC];
        } else {[self showMessage:[responseObject objectForKey:@"msg"]];}
    } failure:^(NSString *error) {
        [self showError:error];
    }];
}

#pragma mark ==========懒加载==========
- (ExaminaColumnReasonView *)reasonView{
    if (!_reasonView) {
        _reasonView = [[ExaminaColumnReasonView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _reasonView.delegate = self;
    }
    return _reasonView;
}
- (ExaminaColumnHeaderWithSectionView *)hederWithOneSectionView{
    if (!_hederWithOneSectionView) {
        _hederWithOneSectionView = [[ExaminaColumnHeaderWithSectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 90*SCALEUNIT) WithType:0 WithTitle:@"科目二待考试名单" WithSection:0];
        [_hederWithOneSectionView.actionBtn addTarget:self action:@selector(ExaminaColumnHeaderWithSectionViewDelegateWithBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _hederWithOneSectionView;
}
- (ExaminaColumnHeaderWithSectionView *)hederWithTwoSectionView{
    if (!_hederWithTwoSectionView) {
        _hederWithTwoSectionView = [[ExaminaColumnHeaderWithSectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 90*SCALEUNIT) WithType:1 WithTitle:@"科目三待考试名单" WithSection:1];
        [_hederWithTwoSectionView.actionBtn addTarget:self action:@selector(ExaminaColumnHeaderWithSectionViewDelegateWithBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _hederWithTwoSectionView;
}
- (ExaminaColumnTopView *)topView{
    if (!_topView) {
        _topView = [[ExaminaColumnTopView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 260*SCALEUNIT)];
        WS(ws);
        _topView.block = ^(NSInteger solution) {
            UIViewController *vc;
            switch (solution) {
                case 0:{
                    ExaminaExamineController *examineVC = [[ExaminaExamineController alloc] init];
                    examineVC.state = 1;
                    vc = examineVC;
                }
                    break;
                case 1:{
                    ExaminaExamineController *examineVC = [[ExaminaExamineController alloc] init];
                    examineVC.state = 2;
                    vc = examineVC;
                }
                    break;
                case 2:{
                    ExaminaWaitExamineController *detailExaminaVC = [[ExaminaWaitExamineController alloc] init];
                    detailExaminaVC.state = 3;
                    vc = detailExaminaVC;
                }
                    break;
                case 3:{
                    ExaminaStatisticsController *statisticsVC = [[ExaminaStatisticsController alloc] init];
                    vc = statisticsVC;
                }
                    break;
                    
                default:
                    break;
            }
            [ws.navigationController pushViewController:vc animated:YES];
        };
    }
    return _topView;
}
- (UITableView *)examinaWithTodayTableView{
    
    if (_examinaWithTodayTableView == nil) {
        _examinaWithTodayTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, SYSTEM_TOP, SCREEN_WIDTH, SCREEN_HEIGHT-SYSTEM_TOP) style:UITableViewStylePlain];
        _examinaWithTodayTableView.tableHeaderView = self.topView;
        _examinaWithTodayTableView.tableFooterView = [[UIView alloc]init];
        _examinaWithTodayTableView.backgroundColor = [UIColor clearColor];
        _examinaWithTodayTableView.delegate = self;
        _examinaWithTodayTableView.dataSource = self;
        _examinaWithTodayTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
        [_examinaWithTodayTableView setSeparatorInset:UIEdgeInsetsMake(0, 30*SCALEUNIT, 0, 30*SCALEUNIT)];
        [_examinaWithTodayTableView setSeparatorColor:RGBA(230, 230, 230, 1)];
        [_examinaWithTodayTableView registerClass:[ExaminaColumnFirstCell class] forCellReuseIdentifier:@"ExaminaColumnFirstCell"];
    }
    return _examinaWithTodayTableView;
}

#pragma mark ==========UITableView代理方法==========
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return section==0?self.subjectTwoArr.count:self.subjectThreeArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 155*SCALEUNIT;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ExaminaColumnFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ExaminaColumnFirstCell" forIndexPath:indexPath];
    TeachManagementModel *model = [indexPath.section==0?self.subjectTwoArr:self.subjectThreeArr objectAtIndex:indexPath.row];
    model.todayExamina = 1;
    cell.model = [indexPath.section==0?self.subjectTwoArr:self.subjectThreeArr objectAtIndex:indexPath.row];
    cell.delegate = self;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return section==0?90*SCALEUNIT:110*SCALEUNIT;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return section==0?self.hederWithOneSectionView:self.hederWithTwoSectionView;
}

#pragma mark ==========ExaminaColumnHeaderWithSectionViewDelegate展开+收起==========
- (void)ExaminaColumnHeaderWithSectionViewDelegateWithBtn:(UIButton *)btn{//btn.selected==NO数据展开操作 YES数据收起操作
    NSInteger section = btn.tag-10000;
    if (!btn.selected) {//收起操作:删除
        if (section-10000==0) {
            self.tempSubjectTwoArr = [NSMutableArray arrayWithArray:section==0?self.subjectTwoArr:self.subjectThreeArr];
        } else {
            self.tempSubjectThreeArr = [NSMutableArray arrayWithArray:section==0?self.subjectTwoArr:self.subjectThreeArr];
        }
        [section==0?self.subjectTwoArr:self.subjectThreeArr removeAllObjects];
        NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:section];
        [self.examinaWithTodayTableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
        [btn setTitle:@"展开" forState:UIControlStateNormal];
    } else {//展开操作:添加
        if (section==0) {
            [self.subjectTwoArr addObjectsFromArray:self.tempSubjectTwoArr];
        } else {
            [self.subjectThreeArr addObjectsFromArray:self.tempSubjectThreeArr];
        }
        NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:section];
        [self.examinaWithTodayTableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
        [btn setTitle:@"收起" forState:UIControlStateNormal];
    }
    btn.selected = !btn.selected;
}

#pragma mark ==========ExaminaColumnFirstCellDelegate(同意+拒绝)==========
- (void)examinaColumnFirstCellDelegateWithState:(NSInteger)state WithID:(long)idNum{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setValue:[NSNumber numberWithInteger:state] forKey:@"state"];
    [dic setValue:[NSNumber numberWithLong:idNum] forKey:@"id"];
    if (state==1) {//同意
        [dic setValue:@"" forKey:@"msg"];
        [[NetworkEngine sharedClient] POSTByToken:EXAMINAMANAGEMENEXAMINEPASSORNOPASS data:dic success:^(id responseObject) {
            if ([[responseObject objectForKey:@"code"] integerValue]==1) {
                [self loadData];
            } else if ([[responseObject objectForKey:@"code"] integerValue]==2) {
                [self toLoginVC];
            } else {
                [self showMessage:[responseObject objectForKey:@"msg"]];
            }
        } failure:^(NSString *error) {
            [self showError:error];
        }];
    } else {//拒绝
        self.resonDic = [NSMutableDictionary dictionaryWithDictionary:dic];
        [self.view addSubview:self.reasonView];
        [self.reasonView doAnimation];
    }
}

#pragma mark ==========ExaminaColumnReasonView(拒绝原因)==========
- (void)trainManageSelectViewDelegateWithReason:(NSString *)content WithState:(BOOL)stateJudge{
    [self.resonDic setValue:content forKey:@"msg"];
    [[NetworkEngine sharedClient] POSTByToken:EXAMINAMANAGEMENEXAMINEPASSORNOPASS data:self.resonDic success:^(id responseObject) {
        if ([[responseObject objectForKey:@"code"] integerValue]==1) {
            [self loadData];
        } else if ([[responseObject objectForKey:@"code"] integerValue]==2) {
            [self toLoginVC];
        } else {
            [self showMessage:[responseObject objectForKey:@"msg"]];
        }
    } failure:^(NSString *error) {
        [self showError:error];
    }];
}

//#pragma mark ==========懒加载==========
//- (FyCalendarView *)calendarView{
//    if (!_calendarView) {
//        _calendarView = [[FyCalendarView alloc] initWithFrame:CGRectMake(0,SYSTEM_TOP, self.view.frame.size.width,100)];
//        _calendarView.date = [NSDate date];
//        _calendarView.calendarBlock = ^(NSInteger day, NSInteger month, NSInteger year){
//            //NSString *dateString=[NSString stringWithFormat:@"%lu-%lu-%lu",year,month,day];
//        };
//    }
//    return _calendarView;
//
//}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
